from odoo import api, fields, models, _


class check_consume(models.Model):
    _name = 'check.consume'
    _description = "增加消费"


    # 选择当前房间
    hou_id = fields.Many2one(
        'check.house',
        '当前房间号',
        required=True,
    )

    # 选择货品
    commodity_ids = fields.One2many(
        'check.commodity',
        'consume_id',
        string='货物名称',
    )

    house_name = fields.Char(
        '当前房号',
        related='hou_id.house_num',
    )

    person_id = fields.Many2one(
        related='hou_id.guest_id',
        string="房间所住宾客",
        store=True,
    )

    # Into_time = fields.Date(
    #     '进店时间',
    #     related='hou_id.create_date',
    # )

    Money = fields.Monetary(
        "押金总额",
        'currency_id',
        readonly=True,
        related='person_id.Money',
    )

    currency_id = fields.Many2one('res.currency',
                                  default=lambda self: self.env.user.company_id.currency_id
                                  )  # price helper

    # 已经消费
    total_consume = fields.Monetary(
        '已消费金额',
        'currency_id',
        related='person_id.Total_consume',
        compute='_compute_money',
    )





    # 消费金额，每个的
    total_money = fields.Monetary(
        '消费金额',
        'currency_id',

    )


    store = fields.Float(
        '库存',
        related='commodity_ids.inventory'
    )


    @api.depends('unit_price', 'num', 'commodity_ids')
    def _compute_money(self):
        for commodity in self.commodity_ids:
            self.total_consume += commodity.unit_price * commodity.inventory










